package com.czu.cy.pages;

import java.text.DateFormat;
import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.czu.cy.dal.CrudServiceDAO;
import com.czu.cy.dal.QueryParameters;
import com.czu.cy.entities.Order;
import com.czu.cy.entities.User;
import com.czu.cy.services.Authenticator;

public class Home {
    @Inject
    private Authenticator authenticator;

    @Inject
    private CrudServiceDAO crudServiceDAO;

    @Property
    private Order order;

    public String getCreationDateString() {
        DateFormat df = DateFormat.getDateTimeInstance();
        return df.format(order.getCreationDate());
    }

    public List<Order> getOrders() {
        if (!authenticator.isLoggedIn()) {
            return null;
        }
        User user = authenticator.getLoggedUser();
        String username = user.getUsername();
        return crudServiceDAO.findWithNamedQuery(Order.BY_USERNAME,
                QueryParameters.with("username", username).parameters());
    }

    public void onActionFromDelete(Long orderId) {
        crudServiceDAO.delete(Order.class, orderId);
    }
}
